// pages/edit_user_info/edit_user_info.js
const app = getApp();
var router = require("../../router.js");
Page({  
    /**
     * 页面的初始数据
     */
    data: {
        nickname: '',
        sexs: [
            '男',
            '女'
        ],
        sex_index: 0,
        date: '',
        img_info: '',
        phone: '',
    },
   
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      
    },
    

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
      app.isLogin();
      var userInfo = wx.getStorageSync('userInfo');      
      this.setData({
        nickname: userInfo.name,
        icon: userInfo.icon,
        sex_index: userInfo.sex-1,
        date: userInfo.birthday,
        phone: userInfo.phone
      });
    },

    change_head_img() {
      var that = this;      
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片      
          wx.uploadFile({
            url: router.user.upload,
            filePath: res.tempFilePaths[0],
            name: 'file',
            formData: {
              uploadType: 'img'
            },
            success: function (res) {                
              var result = JSON.parse(res.data);
              if (result.status) {
                that.setData({
                  icon: result.path,                 
                });
              }
              else {
                wx.showToast({
                  title: '图片上传失败请重试',
                  icon: 'none'
                })
              }
            }
          })
        },
      })
    },

    bindChange(e) {
      this.setData({
        sex_index: e.detail.value
      })
    },

    bindDateChange: function (e) {    
      this.setData({
        date: e.detail.value
      })
    },

    change_name: function (e){
      this.setData({
        nickname: e.detail.value
      })
    }, 
    change_phone: function (e){
      this.setData({
        phone: e.detail.value
      })
    }, 

    open_confirm_box: function(){         
      var that = this;    
      app.request({
        url: router.user.update,
        method: "POST",
        data: {         
          userId: wx.getStorageSync('userInfo').id,
          head: that.data.icon,
          nickname: that.data.nickname,
          sex: that.data.sex_index,
          birthday: that.data.date,
          phone: that.data.phone,
        },
        success: function (data) {         
          if (data.status == true) {
            wx.setStorageSync("userInfo", data.user)
            wx.showToast({
              title: '保存信息成功',
              success:function(){
                wx.navigateBack()
              }
            },1000);           
            
          } else {
            wx.showToast({
              title: '保存信息失败',
              icon: 'none',
              mask: true,
              duration: 2000
            });
          }
        }
      })
    }
})